package com.sina.shop.domain.goods;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.sina.shop.core.domain.Domain;
import com.sina.shop.domain.admin.Admin;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 商品评论实体类
 * 段浩杰   2017-12-11-10:31
 */
@Entity
@Table(name = "tb_goods_comment")
@DynamicUpdate
@DynamicInsert
public class GoodsComment extends Domain {

    @Id
    @Column(name = "id", updatable = false)
    @GenericGenerator(name = "sequence_id", strategy = "com.sina.shop.sequence.IdGenerator")
    @GeneratedValue(generator = "sequence_id")
    @Min(1)
    private Long id;//主键

    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
    @JoinColumn(name = "gid")
    private Goods goods;//商品id

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "goodsComment", fetch = FetchType.LAZY)
    private Set<GoodsCommentPhoto> goodsCommentPhotos;

    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    @JoinColumn(name = "aid")
    private Admin admin;//评论人

    @Column(name = "create_time")
    @ColumnDefault("CURRENT_TIMESTAMP")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime createTime;//创建时间

    @Column(name = "content")
    private String contents;//评论内容

    @Column(name = "bit")
    private Integer bit;//星级

    @Column(name = "type")
    private String type;//类型

    public GoodsComment() {
    }



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    public Integer getBit() {
        return bit;
    }

    public void setBit(Integer bit) {
        this.bit = bit;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Set<GoodsCommentPhoto> getGoodsCommentPhotos() {
        return goodsCommentPhotos;
    }

    public void setGoodsCommentPhotos(Set<GoodsCommentPhoto> goodsCommentPhotos) {
        this.goodsCommentPhotos = goodsCommentPhotos;
    }
}
